#! /usr/bin/php
<?php
/*
 * Created  : Mon 16 Nov 2009 07:48:57 PM EST
 * Modified : Fri 31 Dec 2010 11:25:44 PM EST
 *
 * Copyright 2009 Gautam Iyer <gi1242@nospam.com> (Replace nospam with gmail).
 *
 * This file is part of phpss.
 *
 * phpss is free software: you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later 
 * version.
 *
 * phpss is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 * details.
 *
 * You should have received a copy of the GNU General Public License along with 
 * phpss.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Terminal colors
define( "RE", "\033[0m" );
define( "ER", "\033[0;31m" );	// Red
define( "BD", "\033[0;36m" );	// Cyan
define( "UL", "\033[0;32m" );	// Green
define( "IT", "\033[0;33m" );	// Yellow

// IPTC constants
define( "IPTCKeywords", "2#025" );
define( "IPTCCaption", "2#120" );

// Helper functions
require( "resize.php" );
require( "rcopy.php" );

// Read defaults and process options.
require( "defaults.php" );
include( getenv("HOME") . "/.phpssrc" );
process_options();

if( !isset( $phpss_module ) )
  die( "\$phpss_module not defined.\n" );
require_once( $phpss_module );

/*
 * End of code. Only functions after this.
 */
function process_options()
{
  // Translate short options to long options.
  $opts_withval = array(
    'c' => 'config-file',
    't' => 'title',
    's' => 'subtitle',
    'e' => 'header',
    'C' => 'contact_info',
    'l' => 'logo',
    'q' => 'quote',
    'T' => 'nthreads',
    'o' => 'output_dir',
    'm' => 'phpss_module',
    'd' => 'shared_dir',
    'p' => 'shared_prefix',
    'i' => 'image_dir',
    'b' => 'thumb_dir'
  );

  $sopts = join( ':', array_keys($opts_withval) ) . ':h';
  $lopts = array();
  foreach( array_values( $opts_withval ) as $opt )
    $lopts[] = $opt . ':';
  $lopts[] = 'help';

  // Process command line options.
  if( version_compare( PHP_VERSION, '5.3.0' ) >= 0 )
    $options = getopt( $sopts, $lopts );
  else
  {
    foreach( array_values( $GLOBALS['argv'] ) as $opt )
    {
      if( preg_match( '/--./', $opt ) )
      {
	fwrite( STDERR, ER . "WARNING: PHP_VERSION < 5.3.0. " .
	  "Long options will be ignored.\n" . RE );
	break;
      }
    }
    $options = getopt( $sopts );
  }

  foreach( array_keys($options) as $opt )
  {
    switch( $opt )
    {
      case 'h':
      case 'help':
	print( "See the README for full help.\n" );
	print( "Short options: " . join( ', ', array_keys($opts_withval) ) );
	print( "\nLong options: " . join( ', ', array_values($opts_withval) ) );
	print( "\n" );
	exit();

      case 'c':
      case 'config-file':
	$config_file = $options['c'];
	if( $config_file[0] != "." && $config_file[0] != "/" )
	  $config_file = getcwd() . "/$config_file";
	require_once( $config_file );
	break;


      default:
	$long_opt = array_key_exists( $opt, $opts_withval ) ?
	  $opts_withval[$opt] : $opt;

	$GLOBALS[$long_opt] = $options[$opt];
	break;
    }
  }

  // If array files is still unset, get it from command line, following --
  if( !array_key_exists( 'files', $GLOBALS ) )
  {
    foreach( $GLOBALS['argv'] as $i => $arg )
    {
      if( $arg == "--" )
      {
	$GLOBALS['files'] = array_slice( $GLOBALS['argv'], $i + 1 );
	break;
      }
    }
  }
}
?>
